
#import "TorrentMagicRequest.h"

@implementation TorrentMagicRequest

//
// torrent id
//
@synthesize torrentId														= _torrentId;

//
// user type
//
@synthesize userType														= _userType;

//
// duration
//
@synthesize duration														= _duration;

//
// magic type
//
@synthesize magicType														= _magicType;

//
// test mode
//
@synthesize testMode														= _testMode;

//
// need count
//
@synthesize needCount														= _needCount;

//
// dealloc
//
- (void)dealloc
{
	self.needCount															= nil;
	
	[super dealloc];
}

//
// setup request
//
- (void)setupRequest
{
	//
	// setup rel path
	//
	self.relPath															= self.testMode ? @"promotion.php?test=1" : [NSString stringWithFormat:@"promotion.php?action=magic&torrent=%d", self.torrentId];
	
	//
	// call super
	//
	[super setupRequest];
	
	//
	// setup post data
	//
	NSString* postFormat													= @"action=magic&torrent=%d&user=%@&user_other=&hours=%d&ur=1.00&dr=1.00&promotion=%d&comment=";
	NSString* userType														= self.userType == 0 ? @"ALL" : @"SELF";
	NSString* postString													= [NSString stringWithFormat:postFormat, self.torrentId, userType, self.duration, self.magicType + 2];
	char const* postString8													= [postString UTF8String];
	self.postData															= [NSData dataWithBytes:postString8 length:strlen(postString8)];
}


//
// finished
//
- (void)connectionDidFinishLoading:(NSURLConnection*)connection
{
	if(self.response.statusCode == 200)
		self.needCount														= [[[NSString alloc] initWithData:self.dataBuffer encoding:NSUTF8StringEncoding] autorelease];
	
	[super connectionDidFinishLoading:connection];
}

@end
